package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.RoleAuthorityDAO;
import cn.edu.scau.cmi.hombio.dao.RoleDAO;
import cn.edu.scau.cmi.hombio.dao.RoleEmployeeDAO;

import cn.edu.scau.cmi.hombio.domain.Role;
import cn.edu.scau.cmi.hombio.domain.RoleAuthority;
import cn.edu.scau.cmi.hombio.domain.RoleEmployee;

import cn.edu.scau.cmi.hombio.service.RoleService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for Role entities
 * 
 */

@Controller("RoleRestController")

public class RoleRestController {

	/**
	 * DAO injected by Spring that manages RoleAuthority entities
	 * 
	 */
	@Autowired
	private RoleAuthorityDAO roleAuthorityDAO;

	/**
	 * DAO injected by Spring that manages Role entities
	 * 
	 */
	@Autowired
	private RoleDAO roleDAO;

	/**
	 * DAO injected by Spring that manages RoleEmployee entities
	 * 
	 */
	@Autowired
	private RoleEmployeeDAO roleEmployeeDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for Role entities
	 * 
	 */
	@Autowired
	private RoleService roleService;

	/**
	 * View an existing RoleEmployee entity
	 * 
	 */
	@RequestMapping(value = "/Role/{role_id}/roleEmployees/{roleemployee_id}", method = RequestMethod.GET)
	@ResponseBody
	public RoleEmployee loadRoleRoleEmployees(@PathVariable Integer role_id, @PathVariable Integer related_roleemployees_id) {
		RoleEmployee roleemployee = roleEmployeeDAO.findRoleEmployeeByPrimaryKey(related_roleemployees_id, -1, -1);

		return roleemployee;
	}

	/**
	* Delete an existing RoleAuthority entity
	* 
	*/
	@RequestMapping(value = "/Role/{role_id}/roleAuthorities/{roleauthority_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteRoleRoleAuthorities(@PathVariable Integer role_id, @PathVariable Integer related_roleauthorities_id) {
		roleService.deleteRoleRoleAuthorities(role_id, related_roleauthorities_id);
	}

	/**
	* Save an existing RoleAuthority entity
	* 
	*/
	@RequestMapping(value = "/Role/{role_id}/roleAuthorities", method = RequestMethod.PUT)
	@ResponseBody
	public RoleAuthority saveRoleRoleAuthorities(@PathVariable Integer role_id, @RequestBody RoleAuthority roleauthorities) {
		roleService.saveRoleRoleAuthorities(role_id, roleauthorities);
		return roleAuthorityDAO.findRoleAuthorityByPrimaryKey(roleauthorities.getId());
	}

	/**
	* View an existing RoleAuthority entity
	* 
	*/
	@RequestMapping(value = "/Role/{role_id}/roleAuthorities/{roleauthority_id}", method = RequestMethod.GET)
	@ResponseBody
	public RoleAuthority loadRoleRoleAuthorities(@PathVariable Integer role_id, @PathVariable Integer related_roleauthorities_id) {
		RoleAuthority roleauthority = roleAuthorityDAO.findRoleAuthorityByPrimaryKey(related_roleauthorities_id, -1, -1);

		return roleauthority;
	}

	/**
	* Select an existing Role entity
	* 
	*/
	@RequestMapping(value = "/Role/{role_id}", method = RequestMethod.GET)
	@ResponseBody
	public Role loadRole(@PathVariable Integer role_id) {
		return roleDAO.findRoleByPrimaryKey(role_id);
	}

	/**
	* Save an existing RoleEmployee entity
	* 
	*/
	@RequestMapping(value = "/Role/{role_id}/roleEmployees", method = RequestMethod.PUT)
	@ResponseBody
	public RoleEmployee saveRoleRoleEmployees(@PathVariable Integer role_id, @RequestBody RoleEmployee roleemployees) {
		roleService.saveRoleRoleEmployees(role_id, roleemployees);
		return roleEmployeeDAO.findRoleEmployeeByPrimaryKey(roleemployees.getId());
	}

	/**
	* Show all Role entities
	* 
	*/
	@RequestMapping(value = "/Role", method = RequestMethod.GET)
	@ResponseBody
	public List<Role> listRoles() {
		return new java.util.ArrayList<Role>(roleService.loadRoles());
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* Show all RoleAuthority entities by Role
	* 
	*/
	@RequestMapping(value = "/Role/{role_id}/roleAuthorities", method = RequestMethod.GET)
	@ResponseBody
	public List<RoleAuthority> getRoleRoleAuthorities(@PathVariable Integer role_id) {
		return new java.util.ArrayList<RoleAuthority>(roleDAO.findRoleByPrimaryKey(role_id).getRoleAuthorities());
	}

	/**
	* Create a new Role entity
	* 
	*/
	@RequestMapping(value = "/Role", method = RequestMethod.POST)
	@ResponseBody
	public Role newRole(@RequestBody Role role) {
		roleService.saveRole(role);
		return roleDAO.findRoleByPrimaryKey(role.getId());
	}

	/**
	* Create a new RoleAuthority entity
	* 
	*/
	@RequestMapping(value = "/Role/{role_id}/roleAuthorities", method = RequestMethod.POST)
	@ResponseBody
	public RoleAuthority newRoleRoleAuthorities(@PathVariable Integer role_id, @RequestBody RoleAuthority roleauthority) {
		roleService.saveRoleRoleAuthorities(role_id, roleauthority);
		return roleAuthorityDAO.findRoleAuthorityByPrimaryKey(roleauthority.getId());
	}

	/**
	* Show all RoleEmployee entities by Role
	* 
	*/
	@RequestMapping(value = "/Role/{role_id}/roleEmployees", method = RequestMethod.GET)
	@ResponseBody
	public List<RoleEmployee> getRoleRoleEmployees(@PathVariable Integer role_id) {
		return new java.util.ArrayList<RoleEmployee>(roleDAO.findRoleByPrimaryKey(role_id).getRoleEmployees());
	}

	/**
	* Delete an existing Role entity
	* 
	*/
	@RequestMapping(value = "/Role/{role_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteRole(@PathVariable Integer role_id) {
		Role role = roleDAO.findRoleByPrimaryKey(role_id);
		roleService.deleteRole(role);
	}

	/**
	* Create a new RoleEmployee entity
	* 
	*/
	@RequestMapping(value = "/Role/{role_id}/roleEmployees", method = RequestMethod.POST)
	@ResponseBody
	public RoleEmployee newRoleRoleEmployees(@PathVariable Integer role_id, @RequestBody RoleEmployee roleemployee) {
		roleService.saveRoleRoleEmployees(role_id, roleemployee);
		return roleEmployeeDAO.findRoleEmployeeByPrimaryKey(roleemployee.getId());
	}

	/**
	* Delete an existing RoleEmployee entity
	* 
	*/
	@RequestMapping(value = "/Role/{role_id}/roleEmployees/{roleemployee_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteRoleRoleEmployees(@PathVariable Integer role_id, @PathVariable Integer related_roleemployees_id) {
		roleService.deleteRoleRoleEmployees(role_id, related_roleemployees_id);
	}

	/**
	* Save an existing Role entity
	* 
	*/
	@RequestMapping(value = "/Role", method = RequestMethod.PUT)
	@ResponseBody
	public Role saveRole(@RequestBody Role role) {
		roleService.saveRole(role);
		return roleDAO.findRoleByPrimaryKey(role.getId());
	}
}